package com.cn.why.film.filmindex.controller;

import com.cn.why.film.filmindex.entity.IndexTitle;
import com.cn.why.film.filmindex.entity.PetProduce;
import com.cn.why.film.filmindex.entity.ProOutlineInfomation;
import com.cn.why.film.filmindex.service.PetIndexService;
import com.cn.why.film.filmindex.service.PetMartService;
import com.cn.why.film.filmindex.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宠物店主页Controller
 */
@Controller
@RequestMapping("/petIndex")
public class PetIndexController {
    @Autowired
    private PetIndexService service;
    /***************************************************主页中间信息相关***************************************************************/
    @RequestMapping("/")
    public String jumpToIndex(){
        return "redirect:/petIndex/listIndex";
    }

    /**
     * 展示主页的所有查询信息
     * @param id
     * @return
     */
    @RequestMapping("/listIndex")
    public String listFourIndex(Integer id,Model model){
        //展示主页中间的信息
        List<IndexTitle> list = new ArrayList<>();
        list = service.getIndexFourList(id);
        model.addAttribute("IndexTitles",list);
        //展示右侧导航信息(分页查询)
        List<ProOutlineInfomation> list1Pages = new ArrayList<>();
        list1Pages = service.findNoCriteria(0,8);
        model.addAttribute("pages",list1Pages);
        return "petShop/index";
    }

    /**
     * 购物导航
     * @return
     */
    @RequestMapping("/shopDaoHang")
    @ResponseBody
    public List<ProOutlineInfomation> shopDaoHang(){
        List<ProOutlineInfomation> list = new ArrayList<>();
        list = service.findNoCriteria(0,8);
        return list;
    }

    /**
     * 跳转到对应的商品查询
     */
    @RequestMapping("/jumpToPro")
    public String jumpToPro(HttpSession session, Model model,String proType){
        //先移除session中的list
        session.removeAttribute("list");
        PageUtil<PetProduce> pageUtil = new PageUtil<>();
        //获得list
        List<PetProduce> list = service.getProduceByNumber(service.getProNumber(proType));
        Map<String,Object> map = new HashMap<>();
        //获得map分页后的数据
        map = pageUtil.getPageList(1,list);
        //前台需要的list(分页)
        List<PetProduce> listPetPro = (List<PetProduce>) map.get("list");
        if(listPetPro != null){
            model.addAttribute("listPro",listPetPro);
            //放入model中总页数
            model.addAttribute("countPage",map.get("countPage"));
            //加入session中
            session.setAttribute("list",list);
        }else {
            model.addAttribute("msg","对不起！你搜索的商品不存在！");
        }

        return "petShop/otherHtml/petmart";
    }

    /**
     * 打折商品
     * @return
     */
    @RequestMapping("/discountPro")
    @ResponseBody
    public List<PetProduce> discountPro(){
        List<PetProduce> list = new ArrayList<>();
        //获取list
        int size = service.getDisCountPro().size();
        if(size<8){
            list = service.getDisCountPro().subList(0,size);
        }else {
            list = service.getDisCountPro().subList(size-8,size);
        }
        return list;
    }

    /**
     * 打折商品跳转
     */
    @RequestMapping("/jumpToPro1")
    public String jumpToPro1(HttpSession session, Model model,String proName){
        //先移除session中的list
        session.removeAttribute("list");
        PageUtil<PetProduce> pageUtil = new PageUtil<>();
        //获得list
        List<PetProduce> list = service.getProduceByName(proName);
        Map<String,Object> map = new HashMap<>();
        //获得map分页后的数据
        map = pageUtil.getPageList(1,list);
        //前台需要的list(分页)
        List<PetProduce> listPetPro = (List<PetProduce>) map.get("list");
        if(listPetPro != null){
            model.addAttribute("listPro",listPetPro);
            //放入model中总页数
            model.addAttribute("countPage",map.get("countPage"));
            //加入session中
            session.setAttribute("list",list);
        }else {
            model.addAttribute("msg","对不起！你搜索的商品不存在！");
        }

        return "petShop/otherHtml/petmart";
    }

    /**
     *宠物信息id查询（用于后台管理查询）
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public IndexTitle getById(Integer id){
        return service.findOneById(id);
    }

    /**
     * 用于后台模糊查询（设置添加或修改宠物信息）
     * @return
     */
    @RequestMapping("/getPets")
    @ResponseBody
    public List<IndexTitle> getPetByNames(String texts){
        return service.findByPetName(texts);
    }

}